<script type="text/javascript" charset="utf-8" src="__PUBLIC__/admin/js/uploadify/jquery.uploadify.min.js" ></script>
<link rel="stylesheet" charset="utf-8" type="text/css" href="__PUBLIC__/admin/js/uploadify/uploadify.css"> 
<script type="text/javascript" charset="utf-8" src="__PUBLIC__/admin/js/upload.js" ></script>
<!-- 配置文件 -->
<script type="text/javascript" src="__PUBLIC__/admin/js/ueditor/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="__PUBLIC__/admin/js/ueditor/ueditor.all.js"></script>
<!-- 实例化编辑器 -->
<script type="text/javascript">
	var ue = UE.getEditor('edit');
</script>

<style>
.bg{ background: none; border: none;}
.uploadify-button-text{ display: inline-flex; }
.uploadify-button-text button{ margin: -10px; }
.upviewer img{ height:90px; width: 120px; }
.upviewer ul{ margin:0px; padding:0px; }
.upviewer li{ margin:0px; padding:0px; position: relative; display:inline-block; width:120px; height:90px; background-position: center; background-size: contain; background-repeat: no-repeat; float: left; margin-right:10px; border:1px #ccc solid; background-color:#f0f0f0;}
.upviewer li a{ display:inline-block; position: absolute; bottom:0px; left:0px; text-align:center; width:100%; background-color:rgba(0,0,0,0.3); color:red; cursor: pointer; }
.upviewer li:hover a{ background-color:rgba(0,0,0,0.8); }
</style>
<div class="container" style="padding-top: 50px;">
	<div class="row clearfix">
		<div class="col-md-12 column">
			<form class="form-horizontal" role="form">
				<input type="hidden"  value="{$res.id}" id="id" />
				<div class="form-group">
					 <label for="title" class="col-sm-2 control-label">商品名:</label>
					<div class="col-sm-2">
						<input type="text" class="form-control" id="title" value="{$res.title}" />
					</div>
				</div>
				<div class="form-group">
					 <label for="ds" class="col-sm-2 control-label">描述:</label>
					<div class="col-sm-6">
						<textarea type="text" class="form-control" id="ds" rows="10" >{$res.ds}</textarea>
					</div>
				</div>
				<div class="form-group">
					 <label for="price" class="col-sm-2 control-label">价格:</label>
					<div class="col-sm-2">
						<input type="text" class="form-control" id="price" value="{$res.price}"/>
					</div>
				</div>
				<div class="form-group">
					 <label for="sort" class="col-sm-2 control-label">排序:</label>
					<div class="col-sm-2">
						<input type="text" class="form-control" id="sort" value="{$res.sort}"/>排序越大越前
					</div>
				</div>
				
				<div class="form-group">
					 <label for="inputPassword3" class="col-sm-2 control-label">分类:</label>
					<div class="col-sm-2">
						<select class="form-control" id="cid" >
								<volist name="list" id="li">
									<option value="{$li.id}" <if condition="$li.id eq $res.cid ">selected="selected"</if> >{$li.category}</option>
								</volist>
						</select>
					</div>
				</div>
				
				
				<div class="form-group">
					 <label for="inputPassword3" class="col-sm-2 control-label">(144x122)商品列表图片:</label>
					<div class="col-sm-10">
						<input type="file" class="form-control" id="upload_list" placeholder="照片"><br />    	  
				    	<div id="view_list" class="upviewer" >
					    	<volist  id="list" name="res.listimg" >
					    		<li><img src="{$list}"/><a d_path="{$list}">删除</a></li>
					    	</volist>	
				    	</div>
					</div>
				</div>
				
				<div class="form-group">
					 <label for="inputPassword3" class="col-sm-2 control-label">(640x275)商品详情图片:</label>
					<div class="col-sm-10">
						<input type="file" class="form-control" id="upload_show" placeholder="照片"> <br />  
				    	<div id="view_show" class="upviewer" >
				    		<volist  id="list" name="res.showimg" >
					    		<li><img src="{$list}"/><a d_path="{$list}">删除</a></li>
					    	</volist>	
				    	</div>
					</div>
				</div>
				
				<div class="form-group">
					 <label for="inputPassword3" class="col-sm-2 control-label">商品详情:</label>
					<div class="col-sm-10" >
					<textarea id="edit" name="content" type="text/plain">{$res.content}</textarea>
					</div>
				</div>
				
				<div class="form-group">
					<div class="col-sm-offset-2 col-sm-10">
						 <button type="button" class="btn btn-primary button">保存</button>
					</div>
				</div>
			</form>
		</div>
</div>
</div>
<script>
$(function(){
			
initDelFile();
	//多文件上传
initUploadify({ ID:'upload_list',url:"{:U('Upload/upload')}",success:function(obj){ 
		if(obj.s){
			$('#view_list').append('<li><img src="'+obj.d+'"/><a d_path="'+obj.d+'">删除</a></li>');
			initDelFile();
		}else{
			alert(obj.m);
		}
	} 
});
initUploadify({ ID:'upload_show',url:"{:U('Upload/upload')}",success:function(obj){ 
		if(obj.s){
			$('#view_show').append('<li><img src="'+obj.d+'"/><a d_path="'+obj.d+'">删除</a></li>');
			initDelFile();
		}else{
			alert(obj.m);
		}
	} 
});

	 
function initDelFile(){
	$('.upviewer li a').unbind('click').click(function(){
		if(!confirm('你确认删除吗?')) return false;
		var _this = $(this);
		var dpath = $(_this).attr('d_path');
		if(!dpath) return false;
		$.post("{:U('Upload/delfile')}",{filepath:dpath},function(obj){ 
			if(obj.s){ 
				$(_this).parents('li').remove();
			}else{
				alert(obj.m);
				$(_this).parents('li').remove();
			} 
		},'json');
	});
}
	 
	 $(".button").click(function(){
	 	var id=$('#id').val();
	 	var title = $('#title').val();
	 	var ds = $('#ds').val();
	 	var sort = $('#sort').val();
	 	var price = $('#price').val();
		var cid = $('#cid').val();
	 	var list_src=[];
	 	$("#view_list").find("img").each(function(){
	 		list_src.push($(this).attr('src'));
	 	});
	 	var show_src=[];
	 	$("#view_show").find("img").each(function(){
	 		show_src.push($(this).attr('src'));
	 	});
	 	var content = ue.getContent();
	 	
	 	if(title==""){
	 		alert('请输入商品名');
	 		return false;
	 	}
	 	if(ds==""){
	 		alert('请输入描述');
	 		return false;
	 	}
	 	if(price==""){
	 		alert('请输入价格');
	 		return false;
	 	}
	 	
	 	if($("#view_list").find("img").length!=1){
	 		alert('请上传一张列表图');
	 		return false;
	 	}
	 	if($("#view_show").find("img").length<1){
	 		alert('请上传详情图');
	 		return false;
	 	}
	 	if(content==""){
	 		alert('请输入商品详情');
	 		return false;
	 	}

	 	var url = "{:U('Product/up_product')}";
	 	var parm = {
	 		id:id,
	 		dealer_id:$("#dealer_id option:selected").val(),
	 		brand_id:$("#brand_id option:selected").val(),
	 		title:title,
	 		ds:ds,
	 		price:price,
	 		cid:cid,
	 		list_src:list_src,
	 		show_src:show_src,
	 		content:content,
	 		sort:sort
	 	}
	 //	console.log(parm);
	 	$.post(url,parm,function(obj){
	 		if(obj.s){
	 			alert(obj.m);
	 			location.href="{:U('Product/index')}";
	 		}
	 	})
	 	
	 	
	 })
	 
	 
	});
</script>
